package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.MwRelationPropertyType;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.*;

public class PropertyTypeManage {
    public static Map<String, PropertyType> propertyTypeMap;
    public static Map<String, MwRelationPropertyType> interfacePropertyMap;


    public static List<PropertyTypeView> listPropertyType(Set<PropertyType> excludes) {
        if (null == propertyTypeMap) {
            synchronized (PropertyTypeManage.class) {
                if (null == propertyTypeMap) {
                    init();
                }
            }
        }
        List<PropertyTypeView> list = new ArrayList<>();
        for (PropertyType propertyType : propertyTypeMap.values()) {
            if (excludes.contains(propertyType)) {
                continue;
            }
            PropertyTypeView view = new PropertyTypeView();
            BeanUtils.copyProperties(propertyType, view);
            list.add(view);
        }
        return list;
    }

    public static PropertyType getPropertyType(String propertyId) {
        if (null == propertyTypeMap) {
            synchronized (PropertyTypeManage.class) {
                if (null == propertyTypeMap) {
                    init();
                }
            }
        }

        return propertyTypeMap.get(propertyId);
    }

    public static MwRelationPropertyType getInterfacePropertyType(String propertyId) {
        if (null == interfacePropertyMap) {
            synchronized (PropertyTypeManage.class) {
                if (null == interfacePropertyMap) {
                    init();
                }
            }
        }

        return interfacePropertyMap.get(propertyId);
    }

    public static Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        Criteria criteria = null;
        switch (opsType) {
            case is:
                criteria = new Criteria(name).is(value);
                break;
            case gt:
                criteria = new Criteria(name).gt(value);
                break;
            case gte:
                criteria = new Criteria(name).gte(value);
                break;
            case lt:
                criteria = new Criteria(name).lt(value);
                break;
            case lte:
                criteria = new Criteria(name).lte(value);
                break;
            case exist:
                criteria = new Criteria(name).exists((boolean) value);
                break;
            case ne:
                criteria = new Criteria(name).ne(value);
                break;
            case in:
                criteria = new Criteria(name).in((List) value);
                break;
            case elemMatch:
                criteria = new Criteria(name).elemMatch(new Criteria().in((List) value));
                break;
            case size:
                criteria = new Criteria(name).size(Integer.valueOf(value.toString()));

        }
        return criteria;
    }


    private static void init() {
        propertyTypeMap = new HashMap<>();
        interfacePropertyMap = new HashMap<>();
        PropertyTypeInt propertyTypeInt = new PropertyTypeInt();
        propertyTypeMap.put(propertyTypeInt.getId(), propertyTypeInt);

        PropertyTypeSwitch propertyTypeSwitch = new PropertyTypeSwitch();
        propertyTypeMap.put(propertyTypeSwitch.getId(), propertyTypeSwitch);

        PropertyTypeLong propertyTypeLong = new PropertyTypeLong();
        propertyTypeMap.put(propertyTypeLong.getId(), propertyTypeLong);

        PropertyTypeString propertyTypeString = new PropertyTypeString();
        propertyTypeMap.put(propertyTypeString.getId(), propertyTypeString);

        PropertyTypeTime propertyTypeTime = new PropertyTypeTime();
        propertyTypeMap.put(propertyTypeTime.getId(), propertyTypeTime);

        PropertyTypeIP propertyTypeIP = new PropertyTypeIP();
        propertyTypeMap.put(propertyTypeIP.getId(), propertyTypeIP);

        PropertyTypeList propertyTypeList = new PropertyTypeList();
        propertyTypeMap.put(propertyTypeList.getId(), propertyTypeList);

        PropertyTypeUser propertyTypeUser = new PropertyTypeUser();
        propertyTypeMap.put(propertyTypeUser.getId(), propertyTypeUser);

        PropertyTypeOrg propertyTypeOrg = new PropertyTypeOrg();
        propertyTypeMap.put(propertyTypeOrg.getId(), propertyTypeOrg);

        PropertyTypeGroup propertyTypeGroup = new PropertyTypeGroup();
        propertyTypeMap.put(propertyTypeGroup.getId(), propertyTypeGroup);

        PropertyTypeExtSingle propertyTypeExtSingle = new PropertyTypeExtSingle();
        propertyTypeMap.put(propertyTypeExtSingle.getId(), propertyTypeExtSingle);

        PropertyTypeExtMultiple propertyTypeExtMultiple = new PropertyTypeExtMultiple();
        propertyTypeMap.put(propertyTypeExtMultiple.getId(), propertyTypeExtMultiple);

        PropertyTypeStruct propertyTypeStruct = new PropertyTypeStruct();
        propertyTypeMap.put(propertyTypeStruct.getId(), propertyTypeStruct);

        PropertyTypeEnumMultiple propertyTypeEnumMultiple = new PropertyTypeEnumMultiple();
        propertyTypeMap.put(propertyTypeEnumMultiple.getId(), propertyTypeEnumMultiple);

        PropertyTypeEnumSingle propertyTypeEnumSingle = new PropertyTypeEnumSingle();
        propertyTypeMap.put(propertyTypeEnumSingle.getId(), propertyTypeEnumSingle);

        PropertyTypeMoney propertyTypeMoney = new PropertyTypeMoney();
        propertyTypeMap.put(propertyTypeMoney.getId(), propertyTypeMoney);


        PropertyTypeDate propertyTypeDate = new PropertyTypeDate();
        propertyTypeMap.put(propertyTypeDate.getId(), propertyTypeDate);

        PropertyTypeNumerical propertyTypeFloat = new PropertyTypeNumerical();
        propertyTypeMap.put(propertyTypeFloat.getId(), propertyTypeFloat);

        PropertyTypeLayoutRoom propertyTypeLayoutRoom = new PropertyTypeLayoutRoom();
        propertyTypeMap.put(propertyTypeLayoutRoom.getId(), propertyTypeLayoutRoom);

        PropertyTypeLayoutCabinet propertyTypeLayoutCabinet = new PropertyTypeLayoutCabinet();
        propertyTypeMap.put(propertyTypeLayoutCabinet.getId(), propertyTypeLayoutCabinet);

        PropertyTypeQRCode propertyTypeQRCode = new PropertyTypeQRCode();
        propertyTypeMap.put(propertyTypeQRCode.getId(), propertyTypeQRCode);

        PropertyTypeUploadFile propertyTypeUploadFile = new PropertyTypeUploadFile();
        propertyTypeMap.put(propertyTypeUploadFile.getId(), propertyTypeUploadFile);

        PropertyTypeUploadImg propertyTypeUploadImg = new PropertyTypeUploadImg();
        propertyTypeMap.put(propertyTypeUploadImg.getId(), propertyTypeUploadImg);

        PropertyTypeContract propertyTypeContract = new PropertyTypeContract();
        propertyTypeMap.put(propertyTypeContract.getId(), propertyTypeContract);

        PropertyTypeExtSingle interfaceExtSingle = new PropertyTypeExtSingle();
        interfacePropertyMap.put(interfaceExtSingle.getId(), interfaceExtSingle);

        PropertyTypeExtMultiple interfaceExtMultiple = new PropertyTypeExtMultiple();
        interfacePropertyMap.put(interfaceExtMultiple.getId(), interfaceExtMultiple);

    }

    public static class PropertyTypeView {
        //id
        public String id;

        //名称
        private String name;

        //输入提示
        private String valueTip;

        private int sortNum;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSortNum() {
            return sortNum;
        }

        public void setSortNum(int sortNum) {
            this.sortNum = sortNum;
        }

        public String getValueTip() {
            return valueTip;
        }

        public void setValueTip(String valueTip) {
            this.valueTip = valueTip;
        }
    }
}
